package com.koron.css2.serviceManage.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.RoleBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.RoleMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.mysql.cj.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
* 查询角色信息
* @author DaiSn
* @version 创建时间：2018年10月18日 上午8:27:44
*/
public class RoleList implements ServerInterface {
	private static Logger logger = LogManager.getLogger(RoleList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			// 当前水司编号
			String waterGroup = userInfo.getCurWaterCode();
			List<RoleBean> beans = new ArrayList<RoleBean>();
			RoleMapper mapper = factory.getMapper(RoleMapper.class);
			RoleBean bean = JsonUtils.objectToPojo(req.getData(), RoleBean.class);
			if(StringUtils.isNullOrEmpty(waterGroup)){
				
			}else{
				bean.setGroup(waterGroup);
			}
			
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			beans = mapper.findRole(bean);
			for (RoleBean roleBean : beans) {
				String listRoleUser = mapper.listRoleUser(roleBean.getId());
				if (!StringUtils.isNullOrEmpty(listRoleUser)) {
						// 根据loginid查询name
						String[] strings = listRoleUser.split(",");
					/*	String[] strings = new String[listRoleUser.size()];
						listRoleUser.toArray(strings);*/
						RoleBean t = new RoleBean();
						t.setCodes(strings);
						//roleBean.setOwnName(factory.getMapper(StaffMapper.class).listStaffByLoginId(t));
				}
			}
			info.setCode(Constant.MESSAGE_INT_SUCCESS);
			info.setDescription("success");
			info.setData(new PageInfo<>(beans));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_DBFAIL);
			info.setDescription("数据库异常");
			logger.error("数据库异常", e);
			factory.close(false);
		}
		return info;
	}
}